package com.woyou.ui.component;

import java.util.ArrayList;
import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.ActivityType;
import com.woyou.bean.CodeListResult;
import com.woyou.bean.rpc.LatLngReq;
import com.woyou.model.ShopsModel;
import com.woyou.model.ShopsModel_;
import com.woyou.service.ThreadPoolManager_;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import retrofit.RetrofitError;

/**
 * 活动商家的pop
 * 
 * @author zhou.ni
 *
 */
public class ActivityTypePop{
	
	private static Context mContext;
	private static Activity mActivity;
	private static View mAnchor;
	private static View mBgShade; //背景的遮罩
	private View actScopePopView;
	private ListView mListView;
	
	private static ActivityTypePop instance;
	private List<ActivityType> mList = new ArrayList<ActivityType>();  //数据源
	private List<ActivityType> actList = new ArrayList<ActivityType>();  //获取活动的数据
	private ActTypeAdapter actTypeAdapter;
	
	private PopupWindow mPopupWindow;
	private ShopsModel mShopsController;
	private ActTypeListener mListener;
	private ActivityType removeItem; //每次要移除的item
	
	private boolean isAllShops = false;
	
	public static ActivityTypePop getInstance(Context context, View anchor, View bgShade) {
		mContext = context;
		mActivity = (Activity) context;
		mAnchor = anchor;
		mBgShade = bgShade;
		if ( instance == null ) {
			instance = new ActivityTypePop();
		}
		return instance;
	}
	
	private ActivityTypePop() {
		initView();
	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		mShopsController = ShopsModel_.getInstance_(mContext);
		actScopePopView = View.inflate(mContext, R.layout.pop_act_bar, null);
		mListView = (ListView) actScopePopView.findViewById(R.id.act_listview);
		
		actTypeAdapter = new ActTypeAdapter();
		mListView.setAdapter(actTypeAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				removeItem = mList.get(position);
				if ( mListener!=null ) {
					mListener.itemClick(removeItem);
				}
				refreshList();
				TextView text = (TextView) mAnchor.findViewById(R.id.shops_filter_activities_tv);
				if( !TextUtils.isEmpty(removeItem.getaTypeName()) && removeItem.getaTypeName().equals("全部商家") ){
					text.setText("特色优惠");
				} else {
					text.setText(removeItem.getaTypeName());
				}
				mPopupWindow.dismiss();
			}
		});
		
	}
	
	public void setAllShops(boolean isAllShops) {
		this.isAllShops = isAllShops;
	}

	List<String> allActList = new ArrayList<String>();  //全部优惠的商家id
	ActivityType topItem = null;  //全部优惠的Item
	/**
	 * 加载活动列表
	 * @param latLngReq
	 * @param imageView
	 * @param flag		true 表示要显示全部优惠   false 正常显示列表
	 * @return
	 */
	public void loadActType(final LatLngReq latLngReq, final ImageView imageView) {
		actList.clear();
		
		ActivityType activityType = new ActivityType();
		activityType.setaTypeName("全部商家");
		activityType.setCodeList(new ArrayList<String>());
		actList.add(0, activityType);
		
		mList.clear();
		allActList.clear();
		
		removeItem = null;
		
		ThreadPoolManager_.getInstance_(mContext).executeTask(new Runnable() {
			
			@Override
			public void run() {
				try {
					
					CodeListResult<ActivityType> result = mShopsController.queryActTypeList(latLngReq);
					if ( result!=null && result.getCode() == 1 ) {
						List<ActivityType> list = result.getList();
						
						if ( list!=null && list.size()>0 ){
							
							for (ActivityType type : list) {
								List<String> codeList = type.getCodeList();
								allActList.addAll(codeList);
							}
							
							ActivityType actType = new ActivityType();
							actType.setaTypeName("全部优惠");
							actType.setCodeList(allActList);
							actList.add(1, actType);
							
							topItem = actType;
							
							actList.addAll(list);
							mList.addAll(actList);
							if ( mListener!=null && isAllShops ) {
								mListener.showAllShops(topItem);
							}
							mActivity.runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									imageView.setVisibility(View.VISIBLE);
								}
							});
						} else {
							mActivity.runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									imageView.setVisibility(View.GONE);
								}
							});
						}
					}
				} catch (RetrofitError e) {
					mActivity.runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							imageView.setVisibility(View.GONE);
						}
					});
					switch (e.getKind()) {
					case HTTP:
						break;
					case CONVERSION:
						break;
					case NETWORK:
						break;
					case UNEXPECTED:
						break;
					}
				}	
			}
		});
		
	}
	
	public void showPop() {
		if ( mList==null || mList.size()<3){
			Toast.makeText(mContext, "很遗憾，本区域没有支持优惠活动的商家", 1).show();
			return;
		}
		
		if( mPopupWindow == null ) {
			
			int h = LayoutParams.WRAP_CONTENT;
			if ( mList.size()>10 ){
				h = (int) mContext.getResources().getDimension(R.dimen.dp1100);
			}
			
			mPopupWindow = new PopupWindow(actScopePopView, LayoutParams.WRAP_CONTENT, h);
			mPopupWindow.setOnDismissListener(new OnDismissListener() {
				
				@Override
				public void onDismiss() {
					mBgShade.setVisibility(View.GONE);
				}
			});
		}
		
		mBgShade.setVisibility(View.VISIBLE);
		refreshList();
		
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));// 背景透明
		mPopupWindow.setFocusable(true);
		int h = (int) mContext.getResources().getDimension(R.dimen.dp0);
		mPopupWindow.showAsDropDown(mAnchor, 0, h);
	}

	/**
	 * 刷新列表
	 */
	private void refreshList() {
		if ( removeItem==null )
			removeItem = actList.get(0);
		
		if ( isAllShops ) {
			if ( topItem!=null ) {
				mList.clear();
				mList.addAll(actList);
				mList.remove(topItem);
				isAllShops = false;
			}
		} else {
			mList.clear();
			mList.addAll(actList);
			mList.remove(removeItem);
		}
		
		if ( mList.size() == 0 )
			return;
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				actTypeAdapter.notifyDataSetChanged();
			}
		});
	}


	class ActTypeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if ( convertView == null ) {
				holder = new ViewHolder();
				convertView = View.inflate(mContext, R.layout.item_act_pop, null);
				holder.actText = (TextView) convertView.findViewById(R.id.act_name);
				holder.actNum = (TextView) convertView.findViewById(R.id.act_num);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ActivityType activityType = mList.get(position);
			if ( !TextUtils.isEmpty(activityType.getaTypeName()) ) {
				holder.actText.setText(activityType.getaTypeName());
			}
//			if ( !TextUtils.isEmpty(activityType.getNum()) ) {
//				holder.actNum.setText("(" + activityType.getNum() + ")");
//			}
			return convertView;
		}
		
	}
	
	static class ViewHolder{
		TextView actText;
		TextView actNum;
	}
	
	/**
	 * 隐藏pop
	 */
	public void dismissPop(){
		if ( mPopupWindow!=null && mPopupWindow.isShowing() ){
			mPopupWindow.dismiss();
		}
	}
	
	public interface ActTypeListener{
		void itemClick(ActivityType activityType);
		
		/**
		 * 显示全部优惠
		 * @param activityType
		 */
		void showAllShops(ActivityType activityType);
	}
	
	public void setItemClickListener(ActTypeListener l){
		this.mListener = l;
	}
	
}
